package com.zhiyou100.crm.servlet.message;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhiyou100.crm.service.MessageService;
import com.zhiyou100.crm.service.impl.MessageServiceImpl;
import com.zhiyou100.crm.util.AdminBaseServlet;
import com.zhiyou100.crm.util.Message;
import com.zhiyou100.crm.util.Pager;
import com.zhiyou100.crm.util.User;

/**
 * 收件箱
 */

@WebServlet("/message/listReceive")
public class ListReceiveServlet extends AdminBaseServlet {
	private static final long serialVersionUID = 1L;
	MessageService messageService=new MessageServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		List<Message> messages = messageService.queryMessage();
		List<Message> messages1 = new ArrayList<>();
		User user = (User) request.getSession().getAttribute("user");
		for (int i = 0; i < messages.size(); i++) {
			if ((user.getUser_id()) == (messages.get(i).getReceiver())) {
				messages1.add(messages.get(i));
			}
		}
		if (messages1 != null) {
			//分页显示
			//初始页码
			int start = 1;
			int pagerNo = 1;
			//页面大小，设置
			int pageSize = 3;
			//获取当前页码
			String currentPage1 = request.getParameter("pageNo");
			if (currentPage1 != null) {
				int currentPage2 = Integer.parseInt(currentPage1);
				pagerNo = currentPage2;
			}
			//总数据数，集合的大小
			int total = messages.size();
			//总页数
			int pageCount = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
			Pager pager = new Pager(total, pagerNo, pageSize, pageCount, start);
			List<Message> messages2=new ArrayList<>();
			for (int i = (pagerNo - 1) * pageSize; i < pagerNo * pageSize; i++) {
				if (i == messages1.size()) {
					break;
				}
				messages2.add(messages1.get(i));
			}
			request.setAttribute("pager", pager);
			request.setAttribute("messages", messages2);
			request.getRequestDispatcher("/WEB-INF/view/message/listReceive.jsp").forward(request, response);
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		String field = request.getParameter("searchField");
		String keyword = request.getParameter("keyword");
		List<Message> messages = messageService.queryMessage();
		List<Message> messages1=new ArrayList<>();
		if(messages!=null) {
			for (int i = 0; i < messages.size(); i++) {
				if (messages.get(i).getSubject().contains(keyword)) {
					messages1.add(messages.get(i));
					request.setAttribute("messages",messages1 );
					break;
				}
			}
			request.getRequestDispatcher("/WEB-INF/view/message/listReceive.jsp").forward(request,response);
		}else {
			response.sendRedirect(request.getContextPath()+"/message/listReceive.jsp");
		}
	}

}
